/**
 * @NApiVersion 2.x
 * @NScriptType WorkflowActionScript
 */
define(['N/search', 'N/record', 'N/runtime', '../o2o.client'],
    function(search, record, runtime, o2oClient) {

        function onAction(context) {
            log.debug({title:'text',details:"222"});
            var lang = runtime.getCurrentUser().getPreference('LANGUAGE');
            if (lang == 'zh_CN' || lang == 'zh_TW') {
                var dtl = "\r\n没有面单，请确认操作;";
                var dmt = "\r\n没有母单号，请确认操作;";
                var dsp = "\r\n没有物流商物流方式，无法取消面单！"
                var dmc = "没有找到该物流商匹配的api客户端，请联系管理员;\r\n";
            }else {
                var dtl = "\r\nthe order hasnt tracking label , please confirm your action;";
                var dmt = "\r\nthe order hasnt main tracking number , please confirm your action;";
                var dsp = "\r\nthe order hasnt shipmethod , can not cancel the tracking";
                var dmc = "Carrier doesnt matched api client,please contact the administrator;\r\n";
            }
            var errorMSG = '';


            var rec = context.newRecord;
            var recId = context.newRecord.id;
            var platform = rec.getValue("custrecord_platform_so_platform");
            var platformOrderId = rec.getValue('custrecord_platform_so_order_id');
            var contextText = rec.getValue("custrecord_platform_so_context");
            contextText = JSON.parse(contextText);

            var requestBody = {};
            requestBody.sellerId = contextText.sellerId;
            requestBody.orderId = platformOrderId;
            requestBody.storeName = contextText.storeName;

            log.debug({
                title:'platform',
                details:platform
            });

            var response = o2oClient.method.postApi(
                o2oClient.attributed.URL[platform][o2oClient.attributed.ENV]
                +o2oClient.attributed.URL[platform].GET_ORDER,null,requestBody);

            try{
                if(response.code == 200){
                    var responseBody = JSON.parse(response.body);
                    var orders = responseBody.orders;
                    if(orders.length>0){

                        var order = orders[0];
                        order.platform = responseBody.platform;

                        try{
                            updateSOSource(recId,order);
                        }catch (e){
                            throw "提交最新source失败进record失败"+JSON.stringify(e);
                        }
                    }else{
                        throw "order.length = 0";
                    }
                }else{
                    throw response.body;
                }
            }catch (e){
                log.error({title:'更新source错误'+platform+":"+platformOrderId,
                    details:e});
                throw '更新source错误'+platform+":"+platformOrderId;
            }
        }

        function updateSOSource(sourceId,order){
            try{
                record.submitFields({
                    type: 'customrecord_platform_so_source',
                    id: sourceId,
                    values: {
                        'custrecord_platform_so_context':JSON.stringify(order),
                        'custrecord_platform_so_date':new Date(order.purchaseTime)
                    }
                });
            }catch (e){}
        }


        return {
            onAction: onAction
        };
    });